const util = require('../../../utils/util.js')
const ajax = require('../../../utils/ajax/ajax.js')
Page({
    data: {
        fileList: []
    },
    onLoad: function (options) {
        const alter = options.alter
        this.judgeAlter(alter)
    },
    judgeAlter(alter) {
        const that = this
        if (alter === '修改会员申请凭证') {
            ajax.post({
                url: '/wx/getMyInfo',
                success(res) {
                    const verify_url_arr = res.data.memberVerify.verify_url_arr
                    that.setData({
                        fileList: verify_url_arr.map(item => {
                            return {
                                url: item
                            }
                        })
                    })
                }
            })
        }
    },
    deleteImg(e) {
        const delIndex = e.detail.index
        const fileList = this.data.fileList
        fileList.splice(delIndex, 1)
        this.setData({
            fileList
        })
    },
    afterRead(e) {
        const that = this
        const url = e.detail.file.url
        util.showLoading('图片上传中')
        util.uploadImg({
            url,
            success(res) {
                const fileList = that.data.fileList
                fileList.push({
                    url: res.data
                })
                that.setData({
                    fileList
                })
                util.hideLoading()
            },
            fail() {
                util.toast('图片上传失败')
            },
            complete() {
                util.hideLoading()
            }
        })
    },
    // 提交凭证
    toCommitSuccess() {
        const fileList = this.data.fileList
        if (fileList.length === 0) {
            util.toast('请上传图片凭证')
            return
        }

        let imgs = '';
        for (let i = 0; i < fileList.length; i++) {
            const json = fileList[i].url;
            imgs += json + ',';
        }
        const uploadImg = imgs.substring(0, imgs.lastIndexOf(','))

        ajax.post({
            url: '/wx/submitVerifyUrl',
            data: {
                verify_url: uploadImg
            },
            success() {
                const title = '身份认证'
                wx.navigateTo({
                    url: '/pages/Index/commitSuccess/commitSuccess?title=' + title,
                })
            }
        })
    }
})